<?php
	require 'lib/mysql.php';

	define('NUM_OBJ_ITEM', 20);

	$origin = 'select objitemid, question_stem, optionA, optionB, optionC, optionD, pic_url from objective_item order by rand() limit ' . NUM_OBJ_ITEM;

	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	if(!mysqli_stmt_execute($mysqli_stmt)) {
		die('Statement Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt)); 
	}

	if(!mysqli_stmt_bind_result($mysqli_stmt, $objitem_id, $question_stem, $optionA, $optionB, $optionC, $optionD, $pic_url)) {
		die('Statement Result Binding Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	if(!mysqli_stmt_store_result($mysqli_stmt)) {
		die('Statement Storing Result Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	$i = 0;
	while(mysqli_stmt_fetch($mysqli_stmt)) {
		$i++;
		$itemno = 'objitem' . $i;
		$itemid = 'objitem' . $i . 'id';
?>

	<div class="item">
		<p><?php printf('%d . %s', $i, $question_stem); ?></p>
		<div style="text-align: right">
			<img src="<?=$pic_url ?>" alt="" />
		</div>
		<div class="optgrp">
		<label><input type="radio" name="<?= $itemno ?>" value="A"/>A. <?= $optionA ?></label>
		<label><input type="radio" name="<?= $itemno ?>" value="B"/>B. <?= $optionB ?></label>
		</div>
		<div class="optgrp">
		<label><input type="radio" name="<?= $itemno ?>" value="C"/>C. <?= $optionC ?></label>
		<label><input type="radio" name="<?= $itemno ?>" value="D"/>D. <?= $optionD ?></label>
		</div>
		<input type="hidden" name="<?= $itemid ?>" value="<?= $objitem_id ?>"/>
	</div>

<?php
	}

	mysqli_stmt_close($mysqli_stmt);
	mysqli_close($mysqli);
?>
